package com.dlc.toolkit.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import kotlin.math.abs

open class MyViewPage2 : ViewPager {
    private var mLastX = 0
    private var mLastY = 0

    constructor(context: Context) : super(context)
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        super.onInterceptTouchEvent(ev)

        var isIntercepted: Boolean = false

        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
            }
            MotionEvent.ACTION_MOVE -> {
                if (needEvent(ev)) {
                    isIntercepted = true
                }
            }
            else -> {
            }
        }

        mLastX = ev?.x?.toInt() ?: 0
        mLastY = ev?.y?.toInt() ?: 0

        Log.d("Ctest", "isIntercepted = $isIntercepted")
        return isIntercepted
    }

    private fun needEvent(ev: MotionEvent): Boolean {
        Log.d("Ctest", "lastX = $mLastX ----------------- lastY = $mLastY")
        Log.d("Ctest", "ev.x = ${ev.x} ----------------- ev.y = ${ev.y}")
        Log.d("Ctest", "x = ${abs(ev.x - mLastX)} ----------------- y = ${abs(ev.y - mLastY)}")
        return abs(ev.x - mLastX) > abs(ev.y - mLastY)
    }

}